<template>
  <el-card class="box-card">
    <template #header>
      <div class="card-header">
        <el-form :inline="true" :model="formInline" class="demo-form-inline">
          <el-form-item label="所属仓库">
            <selectStores v-model="formInline.storeId"></selectStores>
          </el-form-item>
          <el-form-item label="所属库区">
            <selectStoreRegion v-model="formInline.storeRegionId"></selectStoreRegion>
          </el-form-item>
          <el-form-item label="库位编码">
            <el-input v-model="formInline.locationSn" placeholder="请输入库位编码" clearable />
          </el-form-item>
          <el-form-item label="所属工厂">
            <selectFactory v-model="formInline.factoryId"></selectFactory>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="search">查找</el-button>
            <el-button type="primary" plain @click="add">新增</el-button>
            <el-button @click="init">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </template>

    <el-table :data="tableParams.rows" style="width: 100%">
      <el-table-column prop="locationSn" label="库位编码" />
      <el-table-column prop="factoryName" label="所属工厂" />
      <el-table-column prop="storeName" label="所属仓库" />
      <el-table-column prop="regionName" label="所属库区" />
      <el-table-column prop="address" label="状态">
        <template #default="scope">
          <el-switch v-model="scope.row.status" @change="switchChange(scope.row)" inline-prompt active-text="启用" inactive-text="禁止" :active-value="0" :inactive-value="1" />
        </template>
      </el-table-column>
      <el-table-column prop="remark" label="备注" />
      <el-table-column prop="address" label="操作" header-align="center" align="center">
        <template #default="scope">
          <el-button type="primary" text @click="handClick(scope.row,1)">编辑</el-button>
          <el-button type="primary" text @click="handClick(scope.row,3)">打印</el-button>
          <el-button type="primary" text @click="handClick(scope.row,2)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <Pagination v-if="tableParams['total']" :total="tableParams['total']" v-model:page="formInline.pageNum" v-model:limit="formInline.pageSize" @pagination="getInfo" />

    <el-dialog v-model="dialogVisible" title="新增/编辑库位 " width="800" :before-close="handleClose">
      <el-form :model="form" label-width="120" :rules="rules" ref="formEl" class="grids">
        <el-form-item label="库位编码" prop="locationSn">
          <el-input v-model="form.locationSn" placeholder="请输入库位编码" clearable disabled />
        </el-form-item>
        <el-form-item label="所属工厂" prop="factoryId">
          <selectFactory v-model="form.factoryId"></selectFactory>
        </el-form-item>
        <el-form-item label="所属仓库" prop="storeId">
          <selectStores v-model="form.storeId" :factoryId="form.factoryId"></selectStores>
        </el-form-item>
        <el-form-item label="所属库区" prop="storeRegionId">
          <selectStoreRegion v-model="form.storeRegionId" :factoryId="form.factoryId" :storeId="form.storeId"></selectStoreRegion>
        </el-form-item>
        <el-form-item label="状态">
          <el-switch v-model="form.status" inline-prompt active-text="启用" inactive-text="禁止" :active-value="0" :inactive-value="1" />
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="form.remark" :autosize="{ minRows: 4 }" type="textarea" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="handleClose">取 消</el-button>
          <el-button type="primary" @click="submitForm" :loading="loading">
            确 认
          </el-button>
        </span>
      </template>
    </el-dialog>
  </el-card>
</template>

<script setup lang="ts">
import { onMounted } from "vue";

const { proxy } = getCurrentInstance();
let formInline = ref({
  pageNum: 1,
  pageSize: 10,
  locationSn: "",
  storeRegionId: "",
  storeId: "",
  factoryId: "",
});
let tableParams = ref({});
let dialogVisible = ref(false);
let loading = ref(false);
let form = ref({
  locationSn: "",
  storeId: "",
  storeRegionId: "",
  status: 0,
  remark: "",
  factoryId: "",
});
let rules = ref({
  locationSn: [
    {
      required: true,
      message: "请输入库位编码",
      trigger: "blur",
    },
  ],
  storeId: [
    {
      required: true,
      message: "请选择所属仓库",
      trigger: "blur",
    },
  ],
  storeRegionId: [
    {
      required: true,
      message: "请选择库区",
      trigger: "blur",
    },
  ],
  factoryId: [
    {
      required: true,
      message: "请选择工厂",
      trigger: "blur",
    },
  ],
});

onMounted(() => {
  search();
});
let search = () => {
  formInline.value.pageNum = 1;
  getInfo();
};
let getInfo = async () => {
  tableParams.value = await proxy.$apis.storeLocationList(formInline.value);
};
let init = () => {
  formInline.value = {
    pageNum: 1,
    pageSize: 10,
    locationSn: "",
    storeRegionId: "",
    storeId: "",
  };
  getInfo();
};
let handleClose = () => {
  form.value = {
    locationSn: "",
    storeId: "",
    storeRegionId: "",
    status: 0,
    remark: "",
    factoryId: "",
  };
  proxy.$refs.formEl.resetFields();
  getInfo();
  dialogVisible.value = false;
};
let submitForm = async () => {
  await proxy.$refs.formEl.validate((valid, fields) => {
    if (valid) {
      save();
    } else {
      for (let key in fields) {
        return proxy.$utils.showToast(fields[key][0].message, 0);
      }
    }
  });
};
let save = async () => {
  try {
    loading.value = true;
    if (form.value.id) {
      await proxy.$apis.storeLocationEdit(form.value);
    } else {
      await proxy.$apis.storeLocationAdd(form.value);
    }
    proxy.$utils.showToast("操作成功");
    handleClose();
  } catch (error) {
  } finally {
    loading.value = false;
  }
};
let handClick = async (e: Object, index: Number) => {
  switch (index) {
    case 1:
      let { data } = await proxy.$apis.storeLocationDetail({ id: e["id"] });
      form.value = proxy.$utils.formatFunction(data, { ...form.value });
      form.value.id = data.id;
      dialogVisible.value = true;
      break;
    case 2:
      let resp = await proxy.$utils.confirmTips(
        `是否确定删除库位编码为${e["locationSn"]}?`
      );
      if (!resp) return;
      await proxy.$apis.storeLocationRemove({ id: e["id"] });
      proxy.$utils.showToast("删除成功");
      getInfo();
      break;
    default:
      break;
  }
};
let switchChange = async (e) => {
  await proxy.$apis.storeLocationEdit({
    id: e.id,
    status: e.status,
  });
  proxy.$utils.showToast("操作成功");
  getInfo();
};
let add = () => {
  form.value.locationSn = `KW-${Date.now()}`;
  dialogVisible.value = true;
};
</script>

<style lang="scss" scoped>
.box-card {
  margin: 20px;
}
.grids {
  display: grid;
  grid-template-columns: repeat(1, minmax(0px, 1fr));
  gap: 8px 12px;
  grid-column-gap: 56rpx; //间隔
}
</style>
